﻿Imports System.IO
Imports System.Xml
Imports System.Windows.Forms
Imports Model

Public Class XMLHelper
    ''' <summary>
    ''' 创建XML文件
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub createXML(ByRef newpro As Project.MyProject)
        Try
            Dim writer As New Xml.XmlTextWriter(newpro.ProDir + "\" + newpro.ProID + newpro.ProName + "\" + "project.pro", System.Text.Encoding.GetEncoding("utf-8"))
            '使用自动缩进便于阅读
            writer.Formatting = Xml.Formatting.Indented
            writer.WriteRaw("<?xml version=""1.0"" encoding=""utf-8"" ?>")
            '书写根元素
            writer.WriteStartElement("project")
            '写基本属性
            writer.WriteAttributeString("name", newpro.ProName)
            writer.WriteAttributeString("id", newpro.ProID)
            writer.WriteAttributeString("dir", newpro.ProDir)
            '添加次级元素_平曲线
            writer.WriteStartElement("midline")
            '添加属性
            writer.WriteAttributeString("num", newpro.ProMidLine.Count.ToString)
            writer.WriteAttributeString("dir", newpro.ProDir + "\" + newpro.ProID + newpro.ProName + "\midline\隧道中线（平）.txt")
            '关闭次级元素_平曲线
            writer.WriteEndElement()
            '添加次级元素_竖曲线
            writer.WriteStartElement("vline")
            '添加属性
            writer.WriteAttributeString("dir", newpro.ProDir + "\" + newpro.ProID + newpro.ProName + "\midline\隧道中线（竖）.txt")
            '关闭次级元素_竖曲线
            writer.WriteEndElement()
            '添加次级元素_点云
            writer.WriteStartElement("ptlist")
            '添加属性
            writer.WriteAttributeString("num", newpro.ProPts.Count.ToString)
            '添加子元素
            For i As Integer = 0 To newpro.ProPts.Count - 1
                writer.WriteElementString("pts" + i.ToString(), newpro.ProDir + "\" + newpro.ProID + newpro.ProName + "\pointcloud\" + Path.GetFileName(newpro.ProPts(i)))
            Next
            '关闭次级元素_点云
            writer.WriteEndElement()
            '关闭根元素
            writer.WriteFullEndElement()
            '将XML写入文件并关闭writer
            writer.Close()
        Catch ex As Exception
            MsgBox(ex.Message & vbCrLf & ex.StackTrace)
        End Try
    End Sub
    ''' <summary>
    ''' 修改XML
    ''' </summary>
    ''' <param name="xmlFileName">要修改的XML文件名</param>
    ''' <param name="rootName">XML文件中的根元素名称</param>
    ''' <param name="elementNameArry">要修改的元素数组</param>
    ''' <param name="innerTextArry">对应于要修改的元素数组的修改文本数组</param>
    ''' <remarks></remarks>
    Public Sub modifXML(ByVal xmlFileName As String, ByVal rootName As String, ByVal elementNameArry() As String, ByVal innerTextArry() As String)
        If My.Computer.FileSystem.FileExists(Application.StartupPath.Trim & "" & xmlFileName) Then
            Dim doc As New Xml.XmlDocument
            doc.Load(Application.StartupPath.Trim & "" & xmlFileName)
            Dim list As Xml.XmlNodeList = doc.SelectSingleNode(rootName).ChildNodes
            For Each xn As Xml.XmlNode In list
                Dim xe As Xml.XmlElement
                xe = xn
                Dim nls As Xml.XmlNodeList = xe.ChildNodes
                For Each xn1 As Xml.XmlNode In nls
                    Dim xe2 As Xml.XmlElement
                    xe2 = xn1
                    For i As Integer = 0 To elementNameArry.Length - 1
                        If xe2.Name = elementNameArry(i) Then
                            xe2.InnerText = innerTextArry(i)
                        End If
                    Next
                Next
            Next
            doc.Save(Application.StartupPath.Trim & "" & xmlFileName)
        End If
    End Sub
    ''' <summary>
    ''' 在XML文件中增加点云节点
    ''' </summary>
    ''' <param name="ptpath"></param>
    ''' <param name="pubpath"></param>
    ''' <remarks></remarks>
    Public Shared Sub addptElement(ByRef ptpath As String, ByRef pubpath As String)
        Dim doc As New Xml.XmlDocument
        doc.Load(pubpath + "\project.pro")
        Dim pts As Xml.XmlNode = doc.SelectSingleNode("project/ptlist")
        Dim i = pts.ChildNodes.Count
        Dim addpt As Xml.XmlElement = doc.CreateElement("pts" + i.ToString())
        addpt.InnerText = pubpath + "\pointcloud\" + Path.GetFileName(ptpath)
        pts.AppendChild(addpt)
        pts.Attributes("num").Value += 1

        doc.Save(pubpath + "\project.pro")
    End Sub
    ''' <summary>
    ''' 读取XML文件
    ''' </summary>
    ''' <param name="xmlFileName">要读取的XML文件名</param>
    ''' <remarks></remarks>
    Public Shared Sub readXML(ByVal xmlFileName As String, ByVal currpro As Project.MyProject)
        Try
            If My.Computer.FileSystem.FileExists(xmlFileName) Then
                Dim doc As New XmlDocument()
                doc.Load(xmlFileName)
                Dim re As Xml.XmlNodeReader = New Xml.XmlNodeReader(doc)
                Dim tmpStr As String = ""
                Dim name As String = ""

                Dim root As XmlElement = doc.DocumentElement
                currpro.ProName = root.Attributes("name").Value
                currpro.ProID = root.Attributes("id").Value
                currpro.ProDir = Path.GetDirectoryName(Path.GetDirectoryName(xmlFileName))
                root.Attributes("dir").Value = currpro.ProDir
                '读取平曲线
                    Dim midline = root.ChildNodes(0)
                    Dim num1 = Integer.Parse(midline.Attributes("num").Value)
                    '读取竖曲线
                    Dim vline = root.ChildNodes(1)
                    '读取点云数据
                    Dim ptlist = root.ChildNodes(2)
                    Dim num2 = Integer.Parse(ptlist.Attributes("num").Value)
                    currpro.ProPts.Clear()
                    For i = 0 To num2 - 1
                        currpro.ProPts.Add(ptlist.ChildNodes(i).InnerText)
                Next
                doc.Save(xmlFileName)
            End If

        Catch ex As Exception
            MsgBox(ex.Message & vbCrLf & ex.StackTrace)
        End Try
    End Sub
End Class
